package com.topvision.gsj.launcher.util;


import com.topvision.gsj.launcher.domain.MyView;

import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;

public class PositionUtil {

    public static boolean isPointInRect(int x, int y, Rect rect) {
        boolean isIn = false;

        isIn = rect.contains(x, y);

        return isIn;
    }

    public static boolean isPointInRectF(float x, float y, RectF rectF) {
        boolean isIn = false;

        isIn = rectF.contains(x, y);

        return isIn;
    }

    public static boolean isPointInView(float x, float y, View view) {
        if (null == view) {
            throw new IllegalArgumentException("view is null!");
        }

        boolean isIn = false;
        
        Rect viewRect = new Rect();  
        view.getGlobalVisibleRect(viewRect);  
        System.out.println(viewRect);  
        
        int top = viewRect.top;
        int left = viewRect.left;
        int right = viewRect.right;
        int bottom = viewRect.bottom;
        int width = viewRect.width();
        int height = viewRect.height();
//        int top = view.getTop();
//        int left = view.getLeft();
//        int right = view.getRight();
//        int bottom = view.getBottom();
//        int width = view.getWidth();
//        int height = view.getHeight();

        if (x >= left && x <= right && y >= top && y <= bottom) {
            isIn = true;
        }
        Log.i("View", ", x: " + x + ", left: " + left + ", right: " + right
                + ", y: " + y + ", top: " + top + ", bottom: " + bottom
                + ", width: " + width + ", height: " + height + ", result: "
                + isIn);
        return isIn;
    }

    /**
     * 判断传入的参数坐标是否属于view
     * @param x
     * @param y
     * @param view
     * @return
     */
	public static int[] isPointInView(float x, float y, MyView view){
		 if (x >= view.getLeft() && x <= view.getRight() && y >= view.getTop() && y <= view.getBottom()) {
	            return new int[]{view.getLeft()+1, view.getTop()+1};
	        }
	        return null;
	}
	
	/**
     * 判断传入的参数坐标是否属于view
     * @param x
     * @param y
     * @param width 悬浮窗的宽度
     * @param height 悬浮窗的高度
     * @param view
     * @return
     */
	public static int[] isPointInView(float x, float y, int width, int height, MyView view){
		float midPointWidth = x + width/2;
		float midPointHeight = y + height/2;
		if (midPointWidth >= view.getLeft() && midPointWidth <= view.getRight()
				&& midPointHeight >= view.getTop()
				&& midPointHeight <= view.getBottom()) {
			return new int[] { view.getLeft() + 1, view.getTop() + 1 };
		}else if(x >= view.getLeft() && x <= view.getRight() && y >= view.getTop() && y <= view.getBottom()){
			return new int[] { view.getLeft() + 1, view.getTop() + 1 };
		}
		return null;
	}
	


	/**
	 * 将view转化成myView
	 * @param view
	 * @return
	 */
	public static MyView getViewInfo(View view) {
        if (null == view) {
        	return null;
        }
        Rect viewRect = new Rect();  
        view.getGlobalVisibleRect(viewRect);  
        
        int top = viewRect.top;
        int left = viewRect.left;
        int right = viewRect.right;
        int bottom = viewRect.bottom;
//        int width = viewRect.width();
//        int height = viewRect.height();
        MyView myview = new MyView();
        myview.setLeft(left);
        myview.setTop(top);
        myview.setRight(right);
        myview.setBottom(bottom);
        return myview;

    }
}
